package com.zhuiYun.project.api.commoditydetail.mapper;

import com.zhuiYun.project.api.commoditydetail.entity.Commodity;
import com.zhuiYun.project.api.handworkdiy.entity.CommodityAndDiyVo;
import com.zhuiYun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CommodityMapper
 * @Description TODO 商品详情dao层接口
 * @Author 赵笛
 * @Date 2023/4/4 16:02
 **/
@Mapper
public interface CommodityMapper extends BaseMapper<Commodity> {

    /**
    * @Description 减商品库存
    * @Author lzy
    * @Date 2023/4/10 10:52
    * @Return java.lang.Integer
    * @param id
    * @param number
    */
    @Update("update commodity set goods_quantity = goods_quantity - #{number} where id = #{id}")
    Integer reduceInventory(@Param("id") Integer id, @Param("number") Integer number);

    /**
     * @Author 赵笛
     * @Description TODO 根据用户id查询当前用户收藏的商品列表
     * @Date 2023/4/11 14:06
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param userId
     * @param commodityName
     */
    @Select("<script> select * " +
            "from commodity " +
            "where id in(" +
            "select commodity_id " +
            "from commodity_collection " +
            "where user_id = #{userId} " +
            "and del_flag = 0) " +
            "and del_flag = 0 " +
            "<when test = 'commodityName !=&quot;&quot; and commodityName !=null'>" +
            "and commodity_name like '%${commodityName}%'</when>" +
            "</script>")
    List<Commodity> getCommodityListByIds(@Param("userId") Integer userId,@Param("commodityName")String commodityName);

    @Select("select * from commodity where id = #{commodityId} and del_flag = 0")
    Commodity getCommodityById(Integer commodityId);

    /**
    * @Description 查询商品信息列表 并查询是否是秒杀商品
    * @Author lzy
    * @Date 2023/4/12 8:16
    * @Return java.util.List<com.zhuiYun.project.api.commoditydetail.entity.Commodity>
    * @param ids
    */
    @Select("<script>" +
            "select c.id,c.commodity_name,c.price,c.vip_price,c.serve,c.goods_quantity,c.back,c.create_time,c.update_time,c.type_id,c.image_id,c.image_view_id,c.del_flag,c.point,c.tag,c.product_purchase_quantity,c.number_of_views,\n" +
            "(case ISNULL(s.seckill_price) when 1 then c.favorable_price when 0 then s.seckill_price end) as favorable_price\n" +
            "from commodity c left join seckill s \n" +
            "on c.id = s.comm_id where " +
            "<foreach item='id' collection='ids' separator='or'>" +
            "c.id = #{id}" +
            "</foreach>" +
            "and c.del_flag = 0" +
            "</script>")
    List<Commodity> getCommodityListCheckSeckill(@Param("ids") ArrayList<String> ids);


    @Update("update commodity set goods_quantity = goods_quantity + #{number} where id = #{id}")
    Integer rollBackInventory(@Param("id") Integer commodityId,@Param("number") Integer commodityQuantity);

    @Select("SELECT id ,commodity_name FROM `commodity` where commodity_name like concat('%',#{selectValue},'%')")
    //查询商品名称（uniapp搜索栏）
    List<CommodityAndDiyVo> getCommodityName(String selectValue);
}
